package org.jeecg.modules.act.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.wx.Encryption;
import org.jeecg.modules.act.dto.ActDictDto;
import org.jeecg.modules.act.entity.ActDict;
import org.jeecg.modules.act.mapper.ActDictMapper;
import org.jeecg.modules.act.service.IActDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 活动事项表
 * @Author: jeecg-boot
 * @Date: 2019-09-04
 * @Version: V1.0
 */
@Service
public class ActDictServiceImpl extends ServiceImpl<ActDictMapper, ActDict> implements IActDictService {


    @Resource
    private ActDictMapper dictMapper;

    @Override
    public Result addDict(ActDictDto dto) {
        Result<ActDict> result = new Result<>();
        if (checkName(dto.getName())) {
            ActDict actDict = new ActDict();
            BeanUtils.copyProperties(dto, actDict);
            actDict.setRegular(0);
            actDict.setDelFlag(0);
            actDict.setVersion(1);
            dictMapper.insert(actDict);
            result.setSuccess(true);
            result.setMessage("新增成功");
            result.setResult(actDict);
        } else {
            result.setMessage("该信息已存在");
            result.setCode(1);
            result.setSuccess(false);
        }
        return result;
    }


    private boolean checkName(String name) {
        QueryWrapper<ActDict> wrapper = new QueryWrapper<>(new ActDict());
        wrapper.eq("name", name);
        ActDict dict = dictMapper.selectOne(wrapper);
        if (dict == null) {
            return true;
        }
        return false;
    }
}
